<!--
* description: 订货参数维护
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicUnFormFields="unFormField"
      :dynamicIsShowSelect="false"
      :dynamicIsInitTable="true"
      :dynamicIsColumnDrop="true"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          class="el-icon-edit"
          :underline="false"
          @click="edit(scope.row.index)"
          style="font-size:12px;"
        >编辑</el-link>
      </template>
    </one-table-template>
    <edit
      :dynamicEditRowData="editRowData"
      :popupsVisible="editPopupsVisible"
      :key="editPopupsKey"
      :popupsState="editPopupsState"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/allocation/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
import store from "@/store";
export default {
  name: "orderParameterSupplier",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Edit: () => import("./edit")
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: wmsApis.queryPartList,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出" //"导出"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        {
          compKey: "btnKey6",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "left",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载" //"模板下载"
        },
        {
          compKey: "btnKey7",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey7",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_ID",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: []
        },
        {
          compKey: "compKey1",
          labelName: "备件编码",
          codeField: "PART_NO",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey2",
          labelName: "备件名称",
          codeField: "PART_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey3",
          labelName: "供应商编号",
          codeField: "SUPPLIER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey4",
          labelName: "ABC分类",
          codeField: "ABC_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "A类",
              value: "A"
            },
            {
              label: "B类",
              value: "B"
            },
            {
              label: "C类",
              value: "C"
            }
          ]
        },
        {
          compKey: "compKey5",
          labelName: "参数完整",
          codeField: "PARAM_INTEGRITY",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "完整",
              value: "1",
            },
            {
              label: "不完整",
              value: "2",
            },
          ],
        },
        {
          compKey: "compKey6",
          labelName: "是否预测",
          codeField: "IF_FORECAST",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          options: [
            {
              label: "是",
              value: "1"
            },
            {
              label: "否",
              value: "0"
            }
          ]
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作",
          codeField: "controlBtn",
          width: 80,
          align: "center",
          isSlot: true
        },
        {
          prop: "PARAM_INTEGRITY",
          label: "参数完整度",
          align: "center",
          width: "120"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          align: "center",
          width: "100"
        },
        {
          prop: "PART_NO",
          label: "备件编码",
          align: "center",
          width: "100"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center",
          width: "100"
        },
        {
          prop: "SUPPLIER_CODE",
          label: "供应商编号",
          align: "center",
          width: "100"
        },
        {
          prop: "IF_FORECAST_STR",
          label: "是否预测",
          align: "center",
          width: "100"
        },
        {
          prop: "ABC_TYPE",
          label: "ABC分类",
          align: "center",
          width: "100"
        },
        {
          prop: "ORDER_PERIOD",
          label: "订单周期",
          align: "center",
          width: "100"
        },
        {
          prop: "ARRIVAL_PERIOD",
          label: "到货周期",
          align: "center",
          width: "100"
        },
        {
          prop: "AMC",
          label: "月均需求数量",
          align: "center",
          width: "100"
        },
        {
          prop: "MONTH_QTY",
          label: "统计月基数",
          align: "center",
          width: "100"
        },
        {
          prop: "SAFETY_FACTOR",
          label: "安全库存调整系数(A)值",
          align: "center",
          width: "100"
        },
        {
          prop: "TARGET_SERVICE_RATE",
          label: "目标服务率",
          align: "center",
          width: "100"
        },
        {
          prop: "SAFETY_STORE",
          label: "安全库存",
          align: "center",
          width: "100"
        },
        {
          prop: "SALE_FLUCTATE_DIFF",
          label: "月销售波动偏差",
          align: "center",
          width: "100"
        },
        {
          prop: "ARRIVE_FLUCTATE_DIFF",
          label: "月到货波动偏差",
          align: "center",
          width: "100"
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_ID: "",
        PART_NO: "",
        PART_NAME: "",
        SUPPLIER_CODE: "",
        ABC_TYPE: "",
        PARAM_INTEGRITY: "",
        IF_FORECAST: ""
      },
      unFormField: {}
    };
  },
  methods: {
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
            this.formField.WAREHOUSE_ID =
            this.formField.WAREHOUSE_ID.substr(
              0,
              this.formField.WAREHOUSE_ID.length - 1
            );
          this.formField.WAREHOUSE_ID = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
        
      });
      
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/warehouse/ppPartList/exporPartList.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "订货参数维护导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    },
    downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/订货参数维护模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    //文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning"
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning"
        });
        return false;
      }
      return true;
    },
    handleChange(param) {
      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const that = this;
      const url = "/mp/wm/vl/warehouse/ppPartList/importExcelToPartList.do";
      that
        .$requestUpload(url, param)
        .then(response => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success"
            });
            if (this.formField.PLAN_ORDER_CODE) {
              that.queryTable();
            }
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {});
    }
  }
};
</script>
<style type="text/css" lang="scss" scoped>
/deep/ .tableTemplate .el-input__inner {
  border: none;
}
</style>